package cn.edu.fzu.homemaking.sso.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.sso.domain.entity.User;
import cn.edu.fzu.homemaking.sso.domain.enums.StatusEnum;
import cn.edu.fzu.homemaking.sso.service.RoleService;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "SSO-角色管理模块")
@RestController
@RequestMapping("/api/auth/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation(value = "查询角色列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", required = true, value = "角色名称"),
            @ApiImplicitParam(name = "status", required = true, value = "状态"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String name, @RequestParam(required = false) StatusEnum status,
                       Integer pageNum, Integer pageSize) {
        return new Result(roleService.list(name, status, pageNum, pageSize));
    }


    @ApiOperation(value = "查询当前用户拥有的角色列表", httpMethod = "GET")
    @RequestMapping("/listCurrentUserRole")
    public Result listCurrentUserRole(HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        return new Result(roleService.listCurrentUserRole(currentUser));
    }


    @ApiOperation(value = "添加角色", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", required = true, value = "角色名称"),
            @ApiImplicitParam(name = "description", required = true, value = "描述") })
    @PostMapping("/add")
    public Result add(@RequestParam String name, @RequestParam String description, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        roleService.add(name, description, currentUser);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "更新角色", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "角色id"),
            @ApiImplicitParam(name = "description", required = false, value = "描述"),
            @ApiImplicitParam(name = "status", required = false, value = "状态") })
    @PostMapping("/update")
    public Result update(@RequestParam Long id, @RequestParam(required = false) String description,
                         @RequestParam(required = false) StatusEnum status, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        roleService.update(id, description, status, currentUser);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "删除角色", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "角色id") })
    @GetMapping("/delete")
    public Result delete(@RequestParam Long id, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        roleService.delete(id, currentUser);
        return Result.SUCCESS;
    }


    /** 授权:分配用户角色 **/
    @ApiOperation(value = "给用户分配角色", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userId", required = true, value = "用户id"),
            @ApiImplicitParam(name = "roleIds", required = true, value = "角色id集合，逗号分割") })
    @PostMapping("/grant")
    public Result grant(@RequestParam Long userId, List<Long> roleIds, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        roleService.grant(userId, roleIds, currentUser);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "根据用户id查询当前用户拥有的角色id", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userId", required = true, value = "资源名称") })
    @GetMapping("/listRoleIdByUserId")
    public Result listRoleIdByUserId(Long userId) {
        return new Result(roleService.listRoleIdByUserId(userId));
    }
}
